package com.xzq.wanandroid.base.activity;

import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xzq.wanandroid.Utils.ActivityManager;
import com.xzq.wanandroid.base.presenter.BasePresenter;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @ClassName : BaseActivity
 * @Description : activity的基类
 * @Author : xzq
 * @Date: 2021-04-26 16:16
 * @Version :1.ActivityManager类管理所初始化的Activity 2.状态栏的初始化控制  3.基础初始化控件的初始化（Butterknife和EventBus） 4.全局的弹窗提示
 * 进行MVP改造
 */
public abstract class BaseActivity <V,P extends BasePresenter<V>> extends AppCompatActivity {
    //创建一个关于activity的线程池，便于管理activity释放内存
    private static WeakReference<Activity> weakReference = null;
    private Unbinder unbinder;
    private P presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(setLayout());
        unbinder = ButterKnife.bind(this);
        presenter = createPresenter();
        if (presenter!=null){
            presenter.attchView((V) this);
        }
        init(savedInstanceState);
    }

    protected abstract void init(Bundle savedInstanceState);

    protected abstract P createPresenter();

    protected abstract int setLayout();


    /**
     * 重写getResources()方法，让APP的字体不受系统设置字体大小影响
     */
    @Override
    public Resources getResources() {
        Resources resources=super.getResources();
        Configuration configuration=new Configuration();
        configuration.setToDefaults();
        resources.updateConfiguration(configuration,resources.getDisplayMetrics());
        return resources;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁的时候销毁activity
        ActivityManager.getmInstance().removeActivity(this);
        //EventBus解绑
        EventBus.getDefault().unregister(this);

    }

}


